home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / animutil / kfast / kfast.lzh / KFAST / src / lines.c < prev    next >
C/C++ Source or Header  |  1992-07-03  |  8KB  |  325 lines

  1. #include "skeleton.h"
  2.  
  3. /* line primitives */
  4. void setbox(line_ptr seg)
  5. {
  6.     int i,x,y;
  7.     
  8.     if((seg==NULL) || (seg->pts==NULL)) return;
  9.     
  10.     seg->box[0][0] = seg->pts->p[0][0];
  11.     seg->box[0][1] = seg->pts->p[0][1];
  12.     seg->box[1][0] = seg->pts->p[0][0];
  13.     seg->box[1][1] = seg->pts->p[0][1];
  14.     
  15.     for(i=1;i<seg->number;i++)
  16.     {
  17.         x = seg->pts->p[i][0];
  18.         y = seg->pts->p[i][1];
  19.         
  20.             if(x<seg->box[0][0]) seg->box[0][0] = x;
  21.             if(x>seg->box[1][0]) seg->box[1][0] = x;
  22.             if(y<seg->box[0][1]) seg->box[0][1] = y;
  23.             if(y>seg->box[1][1]) seg->box[1][1] = y;
  24.     }    
  25. }
  26.  
  27. line_ptr addpoint(line_ptr seg,int x,int y)
  28. {
  29.     if(seg==NULL)
  30.     {
  31.         seg=(line_ptr)malloc(sizeof(struct line));
  32.         seg->number = 1;
  33.         seg->box[0][0] = x;
  34.         seg->box[1][0] = x;
  35.         seg->box[0][1] = y;
  36.         seg->box[1][1] = y;
  37.     seg->linec = 1;
  38.     seg->fillc = 0;
  39.         seg->pts=(struct pts *)malloc(sizeof(struct pts)*POINTS);
  40.         seg->pts->p[0][0]=x;
  41.         seg->pts->p[0][1]=y;
  42.         seg->next = NULL;
  43.     }
  44.     else
  45.     {
  46.         if(x<seg->box[0][0]) seg->box[0][0] = x;
  47.         if(x>seg->box[1][0]) seg->box[1][0] = x;
  48.         if(y<seg->box[0][1]) seg->box[0][1] = y;
  49.         if(y>seg->box[1][1]) seg->box[1][1] = y;
  50.  
  51.         if(seg->number%POINTS==0)
  52.             seg->pts=(struct pts *)realloc((void *)seg->pts,
  53.              sizeof(struct pts)*POINTS*(seg->number/POINTS+1),
  54.          sizeof(struct pts)*POINTS*(seg->number/POINTS));
  55.  
  56.         seg->pts->p[seg->number][0] = x;
  57.         seg->pts->p[seg->number][1] = y;
  58.         seg->number = seg->number + 1;
  59.     }
  60.  
  61.     if(seg->number==1)            /* a single point is entered twice */
  62.     {
  63.         seg->pts->p[1][0]=x; 
  64.         seg->pts->p[1][1]=y;
  65.     }
  66.     return(seg);
  67. }
  68.  
  69. line_ptr appendpoint(line_ptr line,int x,int y)
  70. {
  71.     if(line==NULL) return(addpoint(NULL,x,y));
  72.  
  73.     while(line->next!=NULL)
  74.         line=line->next;
  75.  
  76.     return(addpoint(line,x,y));
  77. }
  78.  
  79. void insertpoint(line_ptr seg,int point,int x,int y)
  80. {
  81.     int xs,ys;
  82.  
  83.     if(seg==NULL) return;
  84.     if(point>=seg->number) return((void)addpoint(seg,x,y));
  85.  
  86.     if(x<seg->box[0][0]) seg->box[0][0] = x;
  87.     if(x>seg->box[1][0]) seg->box[1][0] = x;
  88.     if(y<seg->box[0][1]) seg->box[0][1] = y;
  89.     if(y>seg->box[1][1]) seg->box[1][1] = y;
  90.  
  91.     if(seg->number%POINTS==0)
  92.         seg->pts=(struct pts *)realloc((void *)seg->pts,
  93.          sizeof(struct pts)*POINTS*(seg->number/POINTS+1),
  94.      sizeof(struct pts)*POINTS*(seg->number/POINTS));
  95.  
  96.     for(;point<seg->number;point++)
  97.     {
  98.         xs = seg->pts->p[point][0];
  99.         ys = seg->pts->p[point][1];
  100.         seg->pts->p[point][0] = x;
  101.         seg->pts->p[point][1] = y;
  102.         x = xs;
  103.         y = ys;
  104.     }
  105.  
  106.     seg->pts->p[seg->number][0]=x;
  107.     seg->pts->p[seg->number][1]=y;
  108.     seg->number = seg->number + 1;
  109. }
  110.  
  111. void deletepoint(line_ptr seg,int point)
  112. {
  113.     if(seg==NULL || point>=seg->number || seg->number==0) return;
  114.  
  115.     seg->number = seg->number-1;
  116.  
  117.     for(;point<seg->number;point++)
  118.     {
  119.         seg->pts->p[point][0] = seg->pts->p[point+1][0];
  120.         seg->pts->p[point][1] = seg->pts->p[point+1][1];
  121.     }
  122.  
  123.     if(seg->number%POINTS==0)
  124.         if(seg->number!=0)
  125.             seg->pts=(struct pts *)realloc((void *)seg->pts,
  126.              sizeof(struct pts)*POINTS*(seg->number/POINTS),
  127.          sizeof(struct pts)*POINTS*(seg->number/POINTS+1));
  128.  
  129.     return;
  130. }
  131.  
  132. void movepoint(line_ptr seg,int point,int x,int y)
  133. {
  134.     if(seg==NULL || point>=seg->number) return;
  135.  
  136.     seg->pts->p[point][0] = x;
  137.     seg->pts->p[point][1] = y;
  138.  
  139.     if(x<seg->box[0][0]) seg->box[0][0] = x;
  140.     if(x>seg->box[1][0]) seg->box[1][0] = x;
  141.     if(y<seg->box[0][1]) seg->box[0][1] = y;
  142.     if(y>seg->box[1][1]) seg->box[1][1] = y;
  143.  
  144.     return;
  145. }
  146.  
  147. line_ptr copyline(line_ptr line)
  148. {
  149.     line_ptr newline,seg,last;
  150.  
  151.     newline = NULL;
  152.     last = NULL;
  153.     while(line!=NULL)
  154.     {
  155.         seg=(line_ptr)malloc(sizeof(struct line));
  156.     memcpy((char *)seg,(char *)line,sizeof(struct line));
  157.     if(seg->pts!=NULL)
  158.     {
  159.             seg->pts=(struct pts *)
  160.          malloc(sizeof(struct pts)*POINTS*((seg->number-1)/POINTS+1));
  161.         memcpy((char *)seg->pts,(char *)line->pts,
  162.          seg->number*sizeof(struct pts));
  163.     }
  164.         if(seg->number==1)
  165.         {
  166.         seg->pts->p[1][0] = seg->pts->p[0][0];
  167.         seg->pts->p[1][1] = seg->pts->p[0][1];
  168.         }
  169.             
  170.         if(newline==NULL)
  171.             newline = seg;
  172.         else
  173.             last->next = seg;
  174.         last = seg;
  175.         line = line->next;
  176.     }
  177.     return(newline);
  178. }
  179.  
  180. /*
  181. line_ptr findpoint(line_ptr line,int x,int y,int *point)
  182. {
  183.     while(line!=NULL)
  184.     {
  185.         for(;*point<=line->number;(*point)++)
  186.             if(x==line->pts->p[*point][0] && y==line->pts->p[*point][1])
  187.                 return(line);
  188.         line = line->next;
  189.         *point = 0;
  190.     }
  191.     return(NULL);
  192. }
  193.  */
  194.  
  195. /*
  196. line_ptr findintersect(line_ptr line,int x,int y,int *point)
  197. {
  198.     int x1,y1,x2,y2;
  199.     while(line!=NULL)
  200.     {
  201.         for(;*point<line->number-1;(*point)++)
  202.         {
  203.         x1 = line->pts->p[*point][0];
  204.         y1 = line->pts->p[*point][1];
  205.         x2 = line->pts->p[*point+1][0];
  206.         y2 = line->pts->p[*point+1][1];
  207.  
  208.         if(x>=min(x1,x2) && x<=max(x1,x2) &&
  209.            y>=min(y1,y2) && y<=max(y1,y2) &&
  210.            abs((x1-x2)*(y-y1)-(x-x1)*(y1-y2))<
  211.            min(abs(x1-x2),abs(y1-y2)) )
  212.             return(line);
  213.         }
  214.         line = line->next;
  215.         *point = 0;
  216.     }
  217.     return(NULL);
  218. }
  219.  */
  220.  
  221. line_ptr cutseg(line_ptr seg,int point)
  222. {
  223.     line_ptr newseg;
  224.  
  225.     if(point>=seg->number) return(NULL);
  226.  
  227.     newseg=(line_ptr)malloc(sizeof(struct line));
  228.     memcpy((char *)newseg,(char *)seg,sizeof(struct line));
  229.     newseg->next = NULL;
  230.  
  231.     newseg->number = seg->number-point;
  232.     newseg->pts=(struct pts *)
  233.      malloc(sizeof(struct pts)*POINTS*((newseg->number-1)/POINTS+1));
  234.     memcpy((char *)newseg->pts,(char *)seg->pts->p[point],
  235.      newseg->number*sizeof(struct pts));
  236.     setbox(newseg);
  237.     if(newseg->number==1)
  238.     {
  239.     newseg->pts->p[1][0] = seg->pts->p[0][0];
  240.     newseg->pts->p[1][1] = seg->pts->p[0][1];
  241.     }
  242.  
  243.     if(seg->number/POINTS != (point+1)/POINTS)
  244.         seg->pts=(struct pts *)realloc((void *)seg->pts,
  245.          sizeof(struct pts)*POINTS*(point/POINTS+1),
  246.      sizeof(struct pts)*POINTS*((seg->number-1)/POINTS+1));
  247.     seg->number = point + 1;
  248.     setbox(seg);
  249.     if(seg->number==1)
  250.     {
  251.     seg->pts->p[1][0] = seg->pts->p[0][0];
  252.     seg->pts->p[1][1] = seg->pts->p[0][1];
  253.     }
  254.  
  255.     return(newseg);
  256. }
  257.  
  258. /*
  259. line_ptr deleteseg(line_ptr seg)
  260. {
  261.     line_ptr next;
  262.     if(seg==NULL) return(NULL);
  263.     next = seg->next;
  264.     free(seg->pts,sizeof(struct pts)*POINTS*((seg->number-1)/POINTS+1));
  265.     free(seg,sizeof(struct line));
  266.     return(next);
  267. }
  268.  */
  269.  
  270. /*
  271. line_ptr cutline(line_ptr line,line_ptr seg)
  272. {
  273.     while(line!=NULL && line->next!=seg)
  274.         line=line->next;
  275.     if(line==NULL) return(NULL);
  276.     line->next = NULL;
  277.     return(seg);
  278. }
  279.  */
  280.  
  281. void deleteline(line_ptr line)
  282. {
  283.     line_ptr delete;
  284.  
  285.     while(line!=NULL)
  286.     {
  287.         delete = line;
  288.         line = line->next;
  289.         free(delete->pts,sizeof(struct pts)*POINTS*((delete->number-1)/POINTS+1));
  290.         free(delete,sizeof(struct line));
  291.     }
  292. }
  293.  
  294. void appendline(line_ptr line,line_ptr append)
  295. {
  296.     if(line==NULL) return;
  297.     while(line->next!=NULL)
  298.         line=line->next;
  299.     line->next = append;
  300. }
  301.  
  302. /*
  303. void scaleline(line_ptr line,int xfact,int yfact)
  304. {
  305.     int i;
  306.  
  307.     while(line!=NULL)
  308.     {
  309.         line->box[1][0] = (((line->box[1][0]-line->box[0][0])
  310.          * xfact)>>8) + line->box[0][0];
  311.         line->box[1][1] = (((line->box[1][1]-line->box[0][1])
  312.          * yfact)>>8) + line->box[0][1];
  313.  
  314.         for(i=0;i<line->number;i++)
  315.         {
  316.             line->pts->p[i][0] = (((line->pts->p[i][0]-line->box[0][0])
  317.              * xfact)>>8) + line->box[0][0];
  318.             line->pts->p[i][1] = (((line->pts->p[i][1]-line->box[0][1])
  319.              * yfact)>>8) + line->box[0][1];
  320.         }
  321.         line = line->next;
  322.     }
  323. }
  324.  */
  325.